<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      label-width="80px"
    >
      <el-form-item prop="title" label="标题">
        <el-input v-model="dataForm.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item prop="source" label="来源">
        <ren-select
          v-model="dataForm.source"
          dict-type="warn_info"
          placeholder="请选择来源"
          style="width: 100%"
        ></ren-select>
      </el-form-item>
      <el-form-item prop="publishDate" label="发布日期">
        <el-date-picker v-model="dataForm.publishDate" value-format="yyyy年MM月dd日" placeholder="请选择发布日期" style="width: 100%"></el-date-picker>
      </el-form-item>
      <el-form-item prop="status" label="状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="1">停用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  props: {
  },
  components: {
  },
  data() {
    return {
      visible: false,
      dataForm: {
        category: "预警信息",
        createTime: "",
        creator: "",
        creatorName: "",
        id: "",
        isDel: 0,
        title: "",
        updateTime: "",
        url: "",
        source: "",
        publishDate: "",
        path: "",
        file: [],
        status: 0,
      },
    };
  },
  computed: {
    dataRule() {
      return {
        title: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        url: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        publishDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        file: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`cgjybzHotInfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };

          //处理附件
          if(res.data&&res.data.path){
            let name = res.data.fileName;
            if(!res.data.fileName){
              name = res.data.path.split('/').pop();
            }
            this.dataForm.file = [{
              name: name,
              response: {
                data: {
                  fileUrl: res.data.path,
                  name: name,
                },
              },
            }];
          }
        })
        .catch(() => {});
    },
    fileChange(file){
      this.dataForm.file = file;
      if(file.length>0) this.$refs.dataForm.clearValidate("file");
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (!valid) {
            return false;
          }

          if(this.dataForm.id){
            this.$http
              .post("/cgjybzHotInfo/update", this.dataForm)
              .then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg);
                }
                this.$message({
                  message: this.$t("prompt.success"),
                  type: "success",
                  duration: 500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              })
              .catch(() => {});
          }else{
            this.$http
              .post("/cgjybzHotInfo/save", this.dataForm)
              .then(({ data: res }) => {
                if (res.code !== 0) {
                  return this.$message.error(res.msg);
                }
                this.$message({
                  message: this.$t("prompt.success"),
                  type: "success",
                  duration: 500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              })
              .catch(() => {});
          }
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
